package com.magicodex.bookkeeping.biz.common.util;

/**
 * @author mh.z
 */
public class StringUtil {

    /**
     * 转换成字符串
     *
     * @param source
     * @return
     */
    public static String toString(Object source) {
        if (source == null) {
            return null;
        }

        if (source instanceof String) {
            return (String) source;
        }

        return source.toString();
    }

    /**
     * 当参数的值不是null则返回参数值，否则返回空字符串
     *
     * @param source
     * @return
     */
    public static String orEmpty(Object source) {
        if (source == null) {
            return "";
        }

        String text = toString(source);

        return text;
    }

    /**
     * 去除字符串左右两边的空白符
     *
     * @param text
     * @return
     */
    public static String trim(String text) {
        if (text == null) {
            return null;
        }

        return text.trim();
    }

    /**
     * 判断字符串是否为空
     *
     * @param text
     * @return
     */
    public static boolean isEmpty(CharSequence text) {
        return (text == null || text.length() == 0);
    }

    /**
     * 判断字符串是否不为空
     *
     * @param text
     * @return
     */
    public static boolean isNotEmpty(CharSequence text) {
        return (text != null && text.length() > 0);
    }

}
